package com.yunfei.common.auto.createApiNoteWord;




import com.yunfei.common.auto.GeneratorBase;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import static com.yunfei.common.auto.util.CapitalizeUtil.initcap;


/**
 * 自动生成api接口文档
 * @author  贤云
 *
 */
public class GeneratorApiNoteWord extends GeneratorBase {
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(GeneratorBase.DRIVER);
            String url = GeneratorBase.URL;
            String user = GeneratorBase.NAME;
            String pass = GeneratorBase.PASS;
            conn = DriverManager.getConnection(url,user,pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void main(String[] args){
        GeneratorApiNoteWord generatorInitPageDB=new GeneratorApiNoteWord();
        generatorInitPageDB.generatorInitPage();
    }
    public  void generatorInitPage(){
        Connection conn=getConnection();
        String sql="select * from "+tablename;
        PreparedStatement stmt;
        String content="";
        String queryParam="";//查询参数
        try {
            stmt = conn.prepareStatement(sql);
            ResultSet rs=stmt.executeQuery(sql);
            ResultSetMetaData data=rs.getMetaData();
            DatabaseMetaData dbmd=conn.getMetaData();
            boolean isNext=true;
            while(rs.next()&&isNext){
                isNext=false;
                for(int i = 1 ; i<= data.getColumnCount() ; i++){
                    //获得所有列的数目及实际列数
                    int columnCount=data.getColumnCount();
                    //获得指定列的列名
                    String columnName = data.getColumnName(i);
                    String columnRemark="";
                    String columnRemarkAll="";
                    ResultSet result = dbmd.getColumns(null, "%", tablename, "%");
                    while(result.next()){
                        if(columnName.contains(result.getString("COLUMN_NAME"))){
                            columnRemark= initcap(result.getString("REMARKS"));
                            columnRemarkAll=columnRemark;
                            //以分号分隔，可以添加额外的信息
                            columnRemark=columnRemark.split(";")[0];
                        }
                    }
                    //获得指定列的列值
                    String columnValue = rs.getString(i);
                    //获得指定列的数据类型
                    int columnType=data.getColumnType(i);
                    //获得指定列的数据类型名
                    String columnTypeName=data.getColumnTypeName(i);
                    //所在的Catalog名字
                    String catalogName=data.getCatalogName(i);
                    //对应数据类型的类
                    String columnClassName=data.getColumnClassName(i);
                    //在数据库中类型的最大字符个数
                    int columnDisplaySize=data.getColumnDisplaySize(i);
                    //默认的列的标题
                    String columnLabel= initcap(data.getColumnLabel(i));
                    //获得列的模式
                    String schemaName=data.getSchemaName(i);
                    //某列类型的精确度(类型的长度)
                    int precision= data.getPrecision(i);
                    //小数点后的位数
                    int scale=data.getScale(i);
                    //获取某列对应的表名
                    String tableName=data.getTableName(i);
                    // 是否自动递增
                    boolean isAutoInctement=data.isAutoIncrement(i);
                    //在数据库中是否为货币型
                    boolean isCurrency=data.isCurrency(i);
                    //是否为空
                    int isNullable=data.isNullable(i);
                    //是否为只读
                    boolean isReadOnly=data.isReadOnly(i);
                    //能否出现在where中
                    boolean isSearchable=data.isSearchable(i);
                    //判断是否需要添加查询语句
                    if(i<data.getColumnCount()){
                        queryParam+="" +
                                "\t\t\t\"<div>"+columnLabel+"："+columnRemark+"</div>\"+\n" ;
                    }else{
                        queryParam+="" +
                                "\t\t\t\"<div>"+columnLabel+"："+columnRemark+"</div>\"\n" ;
                    }

                }
            }
            /****************************************自动生成init.jsp页面 start******************************************************************************/

            try {
                content=getInitPageContent(pageTitle, pageOutPath,queryParam);
                File directory = new File("");
                String outputPath = directory.getAbsolutePath()+ "/src/"+ packageOutPath.replace(".", "/")+"/api/"+initcap(tablename) + "Api.java";
                File file=new File(outputPath);
                file.getParentFile().mkdirs();
                FileWriter fw = new FileWriter(outputPath);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(content);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            /****************************************自动生成init.jsp页面 end******************************************************************************/
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }
    }

    /**
     *生成初始化页面
     * @param title 页面标题
     * @param path s9/v3/pastInvestmentCase 页面所在路径
     * @return
     */
    public static  String getInitPageContent(String title,String path,String queryParam){
        String content="";
        content="package demo.controller;\n" +
                "\n" +
                "import com.wordnik.swagger.annotations.Api;\n" +
                "import com.wordnik.swagger.annotations.ApiOperation;\n" +
                "import demo.service.UserMapper;\n" +
                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                "import org.springframework.web.bind.annotation.*;\n" +
                "\n" +
                "    @ApiOperation(httpMethod = \"POST\", value = \""+title+"\" +\n" +
                "            \"<img src=\\\"http://www.51xuediannao.com/uploads/allimg/140104/1-140104201934.gif\\\">\",position = 1,nickname = \""+title+"\", notes = \"\" +\n" +
                "            \"<h4>接口地址："+path+"</h4>\" +\n" +
                "            \"<h4>传递参数</h4>\"+\n" +
                "            \"<div>\" +\n" +
                "            \"pageNow:当前页码 选填<br>\" +\n" +
                "            \"pageSize:每页显示条数 选填<br>\" +\n" +
                "            \"<h4>数据集返回结果-为空时:</h4>\" +\n" +
                "            \"<div class=\\\"block response_body json\\\"><pre class=\\\"json\\\"><code>\"+\n" +
                "            \"{\\n\" +\n" +
                "            \"    \\\"data\\\": null, \\n\" +\n" +
                "            \"    \\\"size\\\": 10, \\n\" +\n" +
                "            \"    \\\"code\\\": 1059, \\n\" +\n" +
                "            \"    \\\"message\\\": \\\"没有更多数据了\\\"\\n\" +\n" +
                "            \"}\"+\n" +
                "            \"<h4>数据集返回结果:</h4>\" +\n" +
                "            \"<div class=\\\"block response_body json\\\"><pre class=\\\"json\\\"><code>\"+\n" +
                "\t\t\t\"\"+\n"+
                "\t\t\t\"</code></pre></div>\"+\n"+
                " \t\t\t\" <h4>返回字段解析</h4>\"+\n";
               content+=queryParam;
               content+= "    )\n" +
                "    @RequestMapping(value = \""+path+"\", method = RequestMethod.POST)\n" +
                "    @ResponseBody\n" +
                "    public Object findList(){\n" +
                "        String myJsonData=\"\";\n" +
                "        return myJsonData;\n" +
                "    }\n" +
                "\n" +
                "}\n";
        return content;
    }
}
